<?php

namespace XWAM\Model;

/**
 * Description of UserModel
 *
 * @author Su 2015-3-5
 */
class UserModel extends AppbaseModel {

    protected $tableName = 't_porg_user';

    /**
     * 重置密码
     * @param string|array $where 查询条件
     * @param string $pwd 新密码
     * @return false|int 成功则返回受影响数量,失败返回false
     * @since 1.0 2015-3-6 by sutroon
     */
    public function updatePassword($uid, $pwd) {
        $result = $this->where(array('id' => $uid))->setField('password', $pwd);
        $log = new LoginLogModel();
        $log->saveUserOperate(false !== $result, "[修改密码%] $uid, password=$pwd", 0, LoginLogModel::ACTION_EDIT);
        return $result;
    }

    /**
     * 保存密码
     * @return type
     * @since 1.0 2016-6-7 SoChishun Added.
     */
    function savePassword() {
        $validator = array(
            array('uid', 'number', '用户无效!', self::MUST_VALIDATE, 'regex', self::MODEL_UPDATE),
            array('password', 'require', '密码无效!', self::MUST_VALIDATE, 'regex', self::MODEL_UPDATE),
            array('repassword', 'require', '确认密码无效!', self::MUST_VALIDATE, 'regex', self::MODEL_UPDATE),
        );
        if (!$this->validate($validator)->create()) {
            return $this->returnMsg(false, $this->getError());
        }
        if ($this->password != $this->repassword) {
            return $this->returnMsg(false, '两次密码输入不一致!');
        }
        $result = $this->where(array('id' => $this->uid))->setField('password', $this->password);
        return $this->returnMsg($result);
    }

    /**
     * 状态值转换
     * @param type $data
     * @return type
     * @since 1.0 2017-3-10
     */
    public static function convertText($data) {
        $astatus = array(
            'status' => array('0' => '待审核', '1' => '正常', '3' => '冻结', '4' => '禁用', '7' => '注销', '14' => '回收站'),
            'online_status' => array('0' => '离线', '1' => '在线', '2' => '隐身', '3' => '离开', '5' => '忙碌'),
            'sex' => array('0' => '女士', '1' => '先生', '2' => '保密'),
            'marital_status' => array('0' => '未婚', '1' => '已婚', '2' => '保密'),
        );
        foreach ($astatus as $field => $texts) {
            if (array_key_exists($field, $data)) {
                $data[$field . '_text'] = $texts[$field];
            }
        }
        return $data;
    }

    // 2017-3-10
    public static function convertTextAll($list) {
        if (!$list) {
            return $list;
        }
        $aout = array();
        foreach ($list as $row) {
            $aout[] = self::convertText($row);
        }
        return $aout;
    }

    function changeStatus($id, $status) {
        $result = $this->where(array('id' => $id))->setField(array('status' => $status));
        return $this->returnMsg($result);
    }

    /**
     * 更改在线状态
     * @param type $id
     * @param type $status
     * @return type
     * @since 1.0 <2015-10-28> SoChishun Added.
     */
    function changeOnlineStatus($id, $status) {
        $result = $this->where(array('id' => $id))->setField(array('online_status' => $status));
        return $this->returnMsg($result);
    }

    /**
     * 保存用户角色
     * @param type $id
     * @param type $role_ids
     * @return type
     */
    function saveRole($aid, $aRoleId) {
        $table = $this->tableName;
        $sqls = array();
        $upIds=array();
        $i = 0;
        foreach ($aRoleId as $roleId) {
            $id = $aid[$i];
            if ($roleId) {
                $upIds[]=$roleId;
                $sqls[] = "update $table set role_id=$roleId where id=$id";
            }
            $i++;
        }
        if (!$sqls) {
            return $this->returnMsg(false, '未选择角色!');
        }
        $mAdv = new \Think\Model\AdvModel();
        $mAdv->patchQuery($sqls);
        RoleModel::updateForeignTitle(false, implode(',', $upIds));
        return $this->returnMsg(true);
    }

    function saveDepartment($aid, $aDepartmentId) {
        $table = $this->tableName;
        $sqls = array();
        $upIds=array();
        $i = 0;
        foreach ($aDepartmentId as $departmentId) {
            $id = $aid[$i];
            if ($departmentId) {
                $upIds[]=$departmentId;
                $sqls[] = "update $table set department_id=$departmentId where id=$id";
            }
            $i++;
        }
        if (!$sqls) {
            return $this->returnMsg(false, '未选择部门!');
        }
        $mAdv = new \Think\Model\AdvModel();
        $mAdv->patchQuery($sqls);
        DepartmentModel::updateForeignTitle(false, implode(',', $upIds));
        return $this->returnMsg(true);
    }

}
